package com.sk.sourcecircle.dialog;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.sk.sourcecircle.R;
import com.sk.sourcecircle.base.BaseDialogFragment;
import com.sk.sourcecircle.callback.MyCallBack;
import com.sk.sourcecircle.entity.GroupListBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by sk on 2017/12/26.
 */

public class ReleaseChooseDialogFragment extends BaseDialogFragment {
    @BindView(R.id.radio_ll)
    LinearLayout radioLl;
    @BindView(R.id.apply)
    TextView apply;
    Unbinder unbinder;
    @BindView(R.id.select_all)
    CheckBox selectAll;

    Unbinder unbinder1;

    private MyCallBack myCallBack;
    List<GroupListBean.DataBean> dataBeanList = new ArrayList<>();

    private List<CheckBox> checkList = new ArrayList<>();
    private List<CheckBox> selectCheckBox = new ArrayList<>();

    public MyCallBack getMyCallBack() {
        return myCallBack;
    }

    public void setMyCallBack(MyCallBack myCallBack) {
        this.myCallBack = myCallBack;
    }

    public List<GroupListBean.DataBean> getDataBeanList() {
        return dataBeanList;
    }

    public void setDataBeanList(List<GroupListBean.DataBean> dataBeanList) {
        this.dataBeanList = dataBeanList;
    }

    @Override
    protected int getContentView() {
        return R.layout.dialog_release_choose_dialog;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        for (final GroupListBean.DataBean dataBean : dataBeanList) {
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_release_group, radioLl, false);
            ImageView head = itemView.findViewById(R.id.group_img);
            final CheckBox checkBox = itemView.findViewById(R.id.group_radio);
            Glide.with(mContext).load(dataBean.getCommunity_image()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(head);
            checkBox.setText(dataBean.getCommunity_name());
            checkBox.setTag(dataBean);
            checkList.add(checkBox);
            radioLl.addView(itemView);
        }

        for (int i = 0; i < checkList.size(); i++) {
            final int finalI = i;
            checkList.get(i).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        selectCheckBox.add(checkList.get(finalI));
                    } else {
                        selectCheckBox.remove(checkList.get(finalI));
                    }
                }
            });
        }

        selectAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if (isChecked) {
                    for (int i = 0; i < checkList.size(); i++) {
                        checkList.get(i).setChecked(true);
                    }
                } else {
                    for (int i = 0; i < checkList.size(); i++) {
                        checkList.get(i).setChecked(false);
                    }
                }

            }
        });
    }

    @OnClick(R.id.apply)
    public void onViewClicked() {
        if (selectCheckBox.size() > 0) {
            dismiss();
            myCallBack.chooseGroup(getReleaseId());
        } else {
            Toast.makeText(mContext, "请选择您要发布的社群！", Toast.LENGTH_SHORT).show();
        }

    }

    private String getReleaseId() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < selectCheckBox.size(); i++) {
            GroupListBean.DataBean tag = (GroupListBean.DataBean) selectCheckBox.get(i).getTag();
            if (i == selectCheckBox.size() - 1) {
                sb.append(tag.getCommunity_id());
            } else {
                sb.append(tag.getCommunity_id()).append(",");
            }
        }
        Log.v("getReleaseId", sb.toString());
        return sb.toString();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder1 = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder1.unbind();
    }
}
